home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / TCPOUT.C < prev    next >
Text File  |  1993-08-09  |  7KB  |  231 lines

  1. #include "global.h"
  2. #include "timer.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "internet.h"
  6. #include "tcp.h"
  7. #include "ip.h"
  8.  
  9. /*
  10.  * this function processes 'real' 32-bit numbers now - DB3FL.920124
  11.  */
  12. static unsigned long int near
  13. backoff1(int n)
  14. {
  15.     unsigned long int j = 1;
  16.  
  17.     return (j <<= (n & 0xffffffffL));        /* Binary exponential back off */
  18. }
  19.  
  20. /* Send a segment on the specified connection. One gets sent only
  21.  * if there is data to be sent or if "force" is non zero
  22.  */
  23. void
  24. tcp_output(struct tcb *tcb)
  25. {
  26.     struct pseudo_header ph;/* Pseudo-header for checksum calcs */
  27.     struct mbuf *hbp,*dbp;    /* Header and data buffer pointers */
  28.     int16 hsize;        /* Size of header */
  29.     struct tcp seg;        /* Local working copy of header */
  30.     int16 ssize;        /* Size of current segment being sent,
  31.                          * including SYN and FIN flags */
  32.     int16 dsize;        /* Size of segment less SYN and FIN */
  33.     int16 usable;        /* Usable window */
  34.     int16 sent;            /* Sequence count (incl SYN/FIN) already in the pipe */
  35.  
  36.     if(tcb == NULLTCB)
  37.         return;
  38.  
  39.     switch(tcb->state){
  40.     case TCP_LISTEN:
  41.     case TCP_CLOSED:
  42.         return;    /* Don't send anything */
  43.     }
  44.     for(;;){
  45.         /* Compute data already in flight */
  46.         sent = tcb->snd.ptr - tcb->snd.una;
  47.  
  48.         /* If transmitter has been idle for more than a RTT,
  49.          * take the congestion window back down to one packet.
  50.          */
  51.         if(!run_timer(&tcb->timer)
  52.          && (msclock() - tcb->lastactive) > tcb->srtt)
  53.             tcb->cwind = tcb->mss;
  54.  
  55.         /* Compute usable send window as minimum of offered
  56.          * and congestion windows, minus data already in flight.
  57.          * Be careful that the window hasn't shrunk --
  58.          * these are unsigned vars.
  59.          */
  60.         usable = min(tcb->snd.wnd,tcb->cwind);
  61.         if(usable > sent)
  62.             usable -= sent;    /* Most common case */
  63.         else if(usable == 0 && sent == 0)
  64.             usable = 1;    /* Closed window probe */
  65.         else
  66.             usable = 0;    /* Window closed or shrunken */
  67.  
  68.         /* Compute size of segment we *could* send. This is the
  69.          * smallest of the usable window, the mss, or the amount
  70.          * we have on hand. (I don't like optimistic windows)
  71.          */
  72.         ssize = min(tcb->sndcnt - sent,usable);
  73.         ssize = min(ssize,tcb->mss);
  74.  
  75.         /* Now we decide if we actually want to send it.
  76.          * Apply John Nagle's "single outstanding segment" rule.
  77.          * If data is already in the pipeline, don't send
  78.          * more unless it is MSS-sized or the very last packet.
  79.          */
  80.         if(sent != 0 && ssize < tcb->mss
  81.          && !(tcb->state == TCP_FINWAIT1 && ssize == tcb->sndcnt-sent)){
  82.             ssize = 0;
  83.         }
  84.          /* Unless the tcp syndata option is on, inhibit data until
  85.          * our SYN has been acked. This ought to be OK, but some
  86.          * old TCPs have problems with data piggybacked on SYNs.
  87.          */
  88.         if(!tcb->flags.synack && !Tcp_syndata){
  89.             if(tcb->snd.ptr == tcb->iss)
  90.                 ssize = min(1,ssize);    /* Send only SYN */
  91.             else
  92.                 ssize = 0;    /* Don't send anything */
  93.         }
  94.         if(ssize == 0 && !tcb->flags.force)
  95.             break;        /* No need to send anything */
  96.  
  97.         tcb->flags.force = 0;    /* Only one forced segment! */
  98.  
  99.         seg.source = tcb->conn.local.port;
  100.         seg.dest = tcb->conn.remote.port;
  101.  
  102.         /* Set the flags according to the state we're in. It is
  103.          * assumed that if this segment is associated with a state
  104.          * transition, then the state change will already have been
  105.          * made. This allows this routine to be called from a
  106.          * retransmission timeout with force=1.
  107.          */
  108.         seg.flags.urg = 0; /* Not used in this implementation */
  109.         seg.flags.rst = 0;
  110.         seg.flags.ack = 1; /* Every state except TCP_SYN_SENT */
  111.         seg.flags.syn = 0; /* syn/fin/psh set later if needed */
  112.         seg.flags.fin = 0;
  113.         seg.flags.psh = 0;
  114.         seg.flags.congest = tcb->flags.congest;
  115.  
  116.         hsize = TCPLEN;    /* Except when SYN being sent */
  117.         seg.mss = 0;
  118.         seg.optlen = 0;
  119.  
  120.         if(tcb->state == TCP_SYN_SENT)
  121.             seg.flags.ack = 0; /* Haven't seen anything yet */
  122.  
  123.         dsize = ssize;
  124.  
  125.         if(!tcb->flags.synack && tcb->snd.ptr == tcb->iss){
  126.             /* Send SYN */
  127.             seg.flags.syn = 1;
  128.             dsize--;    /* SYN isn't really in snd queue */
  129.             /* Also send MSS */
  130.             seg.mss = Tcp_mss;
  131.             seg.optlen = 0;
  132.             hsize = TCPLEN + MSS_LENGTH;
  133.         }
  134.         seg.seq = tcb->snd.ptr;
  135.         seg.ack = tcb->rcv.nxt;
  136.         seg.wnd = tcb->rcv.wnd;
  137.         seg.up = 0;
  138.  
  139.         /* Now try to extract some data from the send queue. Since
  140.          * SYN and FIN occupy sequence space and are reflected in
  141.          * sndcnt but don't actually sit in the send queue, dup_p
  142.          * will return one less than dsize if a FIN needs to be sent.
  143.          */
  144.         if(dsize != 0){
  145.             int16 offset;
  146.  
  147.             /* SYN doesn't actually take up space on the sndq,
  148.              * so take it out of the sent count
  149.              */
  150.             offset = sent;
  151.             if(!tcb->flags.synack && sent != 0)
  152.                 offset--;
  153.  
  154.             if(dup_p(&dbp,tcb->sndq,offset,dsize) != dsize){
  155.                 /* We ran past the end of the send queue;
  156.                  * send a FIN
  157.                  */
  158.                 seg.flags.fin = 1;
  159.                 dsize--;
  160.             }
  161.         } else {
  162.             dbp = NULLBUF;
  163.         }
  164.         /* If the entire send queue will now be in the pipe, set the
  165.          * push flag
  166.          */
  167.         if(dsize != 0 && sent + ssize == tcb->sndcnt)
  168.             seg.flags.psh = 1;
  169.  
  170.         /* If this transmission includes previously transmitted data,
  171.          * snd.nxt will already be past snd.ptr. In this case,
  172.          * compute the amount of retransmitted data and keep score
  173.          */
  174.         if(tcb->snd.ptr < tcb->snd.nxt)
  175.             tcb->resent += min(tcb->snd.nxt - tcb->snd.ptr,ssize);
  176.  
  177.         tcb->snd.ptr += ssize;
  178.         /* If this is the first transmission of a range of sequence
  179.          * numbers, record it so we'll accept acknowledgments
  180.          * for it later
  181.          */
  182.         if(seq_gt(tcb->snd.ptr,tcb->snd.nxt))
  183.             tcb->snd.nxt = tcb->snd.ptr;
  184.  
  185.         /* Fill in fields of pseudo IP header */
  186.         ph.source = tcb->conn.local.address;
  187.         ph.dest = tcb->conn.remote.address;
  188.         ph.protocol = TCP_PTCL;
  189.         ph.length = hsize + dsize;
  190.  
  191.         /* Generate TCP header, compute checksum, and link in data */
  192.         hbp = htontcp(&seg,dbp,&ph);
  193.  
  194.         /* If we're sending some data or flags, start retransmission
  195.          * and round trip timers if they aren't already running.
  196.          */
  197.         if(ssize != 0) {
  198.             /* Set round trip timer */
  199.             unsigned long int trtt = 4 * tcb->mdev + tcb->srtt;
  200.             unsigned long int trt1 = backoff1(tcb->backoff) * trtt;
  201.             unsigned long int trt2 = (backoff1(tcb->backoff) - 1) * trtt;
  202.  
  203.             unsigned long int srtt = (trt1 < trt2) ? trt2 : trt1;
  204.  
  205.             if(srtt > 5 * Tcp_irtt)
  206.                 srtt = 5 * Tcp_irtt;
  207.             if(srtt < 2000)
  208.                 srtt = 2000;
  209.  
  210.             set_timer(&tcb->timer,srtt);
  211.  
  212.             if(!run_timer(&tcb->timer))
  213.                 start_timer(&tcb->timer);
  214.  
  215.             /* If round trip timer isn't running, start it */
  216.             if(!tcb->flags.rtt_run) {
  217.                 tcb->flags.rtt_run = 1;
  218.                 tcb->rtt_time = msclock();
  219.                 tcb->rttseq = tcb->snd.ptr;
  220.             }
  221.         }
  222.         if(tcb->flags.retran)
  223.             tcpRetransSegs++;
  224.         else
  225.             tcpOutSegs++;
  226.  
  227.         ip_send(tcb->conn.local.address,tcb->conn.remote.address,
  228.             TCP_PTCL,tcb->tos,0,hbp,ph.length,0,0);
  229.     }
  230. }
  231.